/*
 * Copyright (c) 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.snj.visangedu.calendar;

import android.view.View;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.snj.visangedu.studytime.StudyPlannerFragment;

// import com.google.api.client.util.DateTime;

// import java.util.Date;

/**
 * @author AA@google.com (Your Name Here)
 *
 */
public class CalendarEventInfo implements Comparable<CalendarEventInfo> {
  public int snID;
  public View view;

  public String id;
  public String summary;
  public String backup; // for server work ..

  public String szColorId;
  public String szDescription;
  public String szEtag;
  public String szKind;
  public String szLocation;

  public String sdt;
  public long sTimeStamp;
  public long sDayStamp;

  public String edt;
  public long eTimeStamp;
  public long eDayStamp;

  public String szResponseHeaders;
  public String szFactory;

  public int iTag;

  CalendarEventInfo(String id, String summary) {
    snID = StudyPlannerFragment.iSerialID;
    StudyPlannerFragment.iSerialID++;

    this.id = id;
    this.summary = summary;
    backup = "";
  }

  CalendarEventInfo(CalendarEventInfo info) {
    snID = StudyPlannerFragment.iSerialID;
    StudyPlannerFragment.iSerialID++;

    Copy(info);
  }

  @Override
  public String toString() {
    return summary;
  }

  public int compareTo(CalendarEventInfo other) {
    return summary.compareTo(other.summary);
  }

  public void Copy(CalendarEventInfo info) {
    id = info.id;
    summary = info.summary;

    szColorId = info.szColorId;
    szDescription = info.szDescription;
    szEtag = info.szEtag;
    szKind = info.szKind;
    szLocation = info.szLocation;

    sdt = info.sdt;
    sTimeStamp = info.sTimeStamp;
    sDayStamp = info.sDayStamp;

    edt = info.edt;
    eTimeStamp = info.eTimeStamp;
    eDayStamp = info.eDayStamp;

    backup = info.backup;

    szResponseHeaders = info.szResponseHeaders;
    szFactory = info.szFactory;
  }

  public void AutoSetTimeStamp() {
    // yyyy-MM-dd HH:mm
    Date date = StringToDate(sdt);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    sTimeStamp = cal.getTimeInMillis();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    sDayStamp = cal.getTimeInMillis();

    // yyyy-MM-dd HH:mm
    date = StringToDate(edt);
    cal.setTime(date);
    eTimeStamp = cal.getTimeInMillis();

    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    eDayStamp = cal.getTimeInMillis();

  }

  public boolean isAcceptEvent(long timeStamp) {
    if (timeStamp >= sDayStamp && timeStamp < eDayStamp) {
      return true;
    }
    return false;
  }

  public Date StringToDate(String dateText) {
    Date date = null;

    try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
      date = sdf.parse(dateText);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }

}
